package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseCategory;
import com.maoye.ossp.sys.base.service.ISysBaseCategoryService;

/**
 * 通用类别Controller
 * 
 * @author yangyf
 * 
 *         2014年1月21日
 */
@Controller
@RequestMapping(value = "/sys/base/")
public class SysBaseCategoryController extends BaseController {
	@Resource
	private ISysBaseCategoryService baseCategoryService;

	/**
	 * 根据父获取子节点
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "json")
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setCharacterEncoding("utf-8");
		String contextPath = request.getContextPath();
		String pId = request.getParameter("treeNodeId");
		Map map = new HashMap();
		map.put("pid", pId);
		List<SysBaseCategory> list = baseCategoryService.getByPId(map);
		PrintWriter out = response.getWriter();
		StringBuffer json = new StringBuffer();

		json.append("[");
		for (int i = 0; i < list.size(); i++) {
			SysBaseCategory baseCategory = list.get(i);

			Long pid = baseCategory.getParent().getId();

			json.append("{").append("id:").append(baseCategory.getId())
					.append(",");
			json.append("pId:").append(pid).append(",");
			json.append("name:\"").append(baseCategory.getName()).append("\"")
					.append(",");
			json.append("isParent:").append(
					baseCategory.getChildNodes().size() < 1 ? 0 : 1);
			json.append(",");
			json.append("open:").append("true");
			json.append("}");
			if (i < list.size() - 1) {
				json.append(",");
			}
		}
		json.append("]");
		out.write(json.toString());
		out.flush();
		out.close();

	}

	/**
	 * 得到根节点
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "getNode")
	public void getParentNode(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String contextPath = request.getContextPath();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		StringBuffer json = new StringBuffer();
		Map map = new HashMap();
		List<SysBaseCategory> list = baseCategoryService
				.getSysBaseCategory(map);
		json.append("[");
		for (int i = 0; i < list.size(); i++) {
			SysBaseCategory baseCategory = list.get(i);
			Long pid = null;
			if (baseCategory.getParent() == null) {
				pid = 0L;

				json.append("{").append("id:").append(baseCategory.getId())
						.append(",");
				json.append("name:\"").append(baseCategory.getName())
						.append("\"").append(",");
				json.append("pId:").append(pid).append(",");
				json.append("isParent:").append(1);
				json.append(",");
				json.append("true:").append("true");
				json.append("}");
				if (i < list.size() - 1) {
					json.append(",");
				}
			}
		}
		json.append("]");
		out.write(json.toString());
		out.flush();
		out.close();

	}

	/**
	 * 类别显示页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "show")
	public String getId() {
		return "/sys/base/category/categorylist";

	}

	/**
	 * 添加类别
	 * 
	 * @param sysBaseCategory
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "add")
	public String add(SysBaseCategory sysCategory, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		String treeId = request.getParameter("treeid");
		if (!treeId.equals("undefined") && treeId != null) {
			SysBaseCategory sysBaseCategory = baseCategoryService
					.getById(treeId);
			sysCategory.setParent(sysBaseCategory);
			sysCategory.setCreateBy("zhangsan");
			sysCategory.setCreateTime(new Date());
			model.addAttribute("sysCategory", sysCategory);
		}
		return "/sys/base/category/add";

	}

	/**
	 * 修改类别
	 * 
	 * @param model
	 * @param redirectAttributes
	 * @param request
	 * @return
	 */

	@RequestMapping(value = "edit")
	public String edit(SysBaseCategory sysBaseCategory, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request) {
		String categoryId = request.getParameter("treeid");
		if (!categoryId.equals("undefined") && categoryId != null) {
			SysBaseCategory sysCategory = baseCategoryService
					.getById(categoryId);
			model.addAttribute("sysCategory", sysCategory);
		}
		return "/sys/base/category/update";
	}

	/**
	 * 保存修改的
	 * 
	 * @param sysBaseCategory
	 * @param model
	 * @param redirectAttributes
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "saves")
	public String saves(SysBaseCategory sysBaseCategory, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request) {
		String byId = request.getParameter("byId");
		if (!byId.equals("") || byId != null) {
			long id = Long.valueOf(byId);
			sysBaseCategory.setId(id);
			sysBaseCategory.setUpdateBy("lll");
			sysBaseCategory.setUpdateTime(new Date());
			baseCategoryService.update(sysBaseCategory);
		}
		addMessage(redirectAttributes, "类别修改'" + sysBaseCategory.getName()
				+ "'成功");
		return "redirect:/sys/base/show";

	}

	/**
	 * 保存类别
	 * 
	 * @param sysBaseCategory
	 * @param model
	 * @param redirectAttributes
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "save")
	public String save(SysBaseCategory sysBaseCategory, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request) {
		String parentid = request.getParameter("parentid");

		SysBaseCategory sysCategory = null;
		if (!parentid.equals("") || parentid != null) {
			sysCategory = baseCategoryService.getById(parentid);// 取父节点
			sysBaseCategory.setParent(sysCategory);

			if (sysBaseCategory.getId() == null || sysBaseCategory.getId() == 0) {
				sysBaseCategory.setId(idGenerator.getUId());
				sysBaseCategory.setCreateBy("zhangsan");
				sysBaseCategory.setParentid(parentid);
				sysBaseCategory.setCreateTime(new Date());
				baseCategoryService.create(sysBaseCategory);

			}
		}
		addMessage(redirectAttributes, "类别保存'" + sysBaseCategory.getName()
				+ "'成功");
		return "redirect:/sys/base/show";

	}

	/**
	 * 删除类别
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "remove")
	public void remove(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		HashMap map = new HashMap();
		String treeId = request.getParameter("treeId");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if (!treeId.equals("undefined") && treeId != null) {
			SysBaseCategory sysBaseCategory = baseCategoryService
					.getById(treeId);
			
		}
	}

	public ISysBaseCategoryService getBaseCategoryService() {
		return baseCategoryService;
	}

	public void setBaseCategoryService(
			ISysBaseCategoryService baseCategoryService) {
		this.baseCategoryService = baseCategoryService;
	}

}
